﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Ext.Net;

public partial class UserControl_UCMoreFile : System.Web.UI.UserControl
{
    #region 定义属性

    public string ControlName
    {
        get
        {
            if (ViewState["ControlName"] == null)
                return "UCMoreFile1";
            else
                return ViewState["ControlName"].ToString();
        }
        set
        {
            ViewState["ControlName"] = value;
        }
    }

    public string ServerFolder
    {
        get
        {
            return CommonLib.ComMon.BSconfig.GetConfig("AttachFileDir");
        }
        set
        {

        }
    }

    /// <summary>
    /// (上传文件数量)设置为1时只允许上传1个[同时选择文件数默认也只能是1个],设置为0时上传数量没有上限


    /// </summary>
    public int UpLimit
    {
        get
        {
            if (ViewState["UpLimit"] == null)
                return 0;
            else
            {
                if (ViewState["UpLimit"].ToString() == "1")
                    btnAction = "one";
                return int.Parse(ViewState["UpLimit"].ToString());
            }
        }
        set
        {
            ViewState["UpLimit"] = value;
        }
    }
    /// <summary>
    /// (选择上传文件数量)设置为more时允许选择多个,设置为one时允许选择1个,默认为多个


    /// </summary>
    public string btnAction
    {
        get
        {
            if (ViewState["btnAction"] == null)
                return "SWFUpload.BUTTON_ACTION.SELECT_FILES";
            else
            {
                if (ViewState["btnAction"].ToString() == "more")
                    return "SWFUpload.BUTTON_ACTION.SELECT_FILES";
                else
                    return "SWFUpload.BUTTON_ACTION.SELECT_FILE";
            }
        }
        set
        {
            ViewState["btnAction"] = value;
        }
    }
    /// <summary>
    /// 设置为true时为只读(不可操作移除功能),为false时为可选(可操作移除功能)
    /// </summary>
    public string Setreadonly
    {
        get
        {
            if (ViewState["Setreadonly"] == null)
                return "false";
            else
                if (ViewState["Setreadonly"].ToString().ToUpper() == "FALSE")
                return "false";
            else
                return "true";
        }
        set
        {
            ViewState["Setreadonly"] = value;
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ControlName = this.ClientID;
            this.Page.Form.Enctype = "multipart/form-data";
            this.Page.Form.Method = "post";
        }
    }
    /// <summary>
    /// 解析数据库中的字符串（查看时调用）





    /// </summary>
    /// <param name="xmlString"></param>
    public void GetXmlToPage(string xmlString)
    {
        //string _folder = CommonLib.ComMon.BSconfig.GetConfig("AttachFileDir");

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        string res = "";

        try
        {
            ds.ReadXml(new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlString)));
            dt = ds.Tables[0];
            string name = "";
            string realName = "";
            string fileSize = "";
            string resGroup = "";
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["path"] as string != "")
                {
                    name = dr["name"].ToString();
                    realName = dr["path"].ToString();
                    fileSize = dr["size"].ToString();
                    resGroup = name + "|" + realName + "|" + fileSize;
                }
                if (res != "")
                    res = res + "," + resGroup;
                else
                    res = resGroup;
            }
            if (res != "")
                extBtnResh.AddScript("BindRecordByUpd('" + res + "');");//进入编辑页面时

            else
                extBtnResh.AddScript("DeleteAttar();");

        }
        catch (Exception)
        {
            extBtnResh.AddScript("DeleteAttar();");
        }
    }

    /// <summary>
    /// 数据库保存成功后调用
    /// </summary>
    public void DeleteFile()
    {
        //string _folder = CommonLib.ComMon.BSconfig.GetConfig("AttachFileDir");
        string[] saveFilesInfo = Hdn_SaveFilesInfo.Value.Split(',');
        string fileRealName = "";
        try
        {
            if (Hdn_SaveFilesInfo.Value != "" && saveFilesInfo.Length > 0)
            {
                string[] oneFileArr = null;
                for (int i = 0; i < saveFilesInfo.Length; i++)
                {
                    oneFileArr = saveFilesInfo[i].Split('|');
                    fileRealName = oneFileArr[1];
                    File.Delete(Server.MapPath("../TmpFile/") + fileRealName);
                }
            }
        }
        catch
        {
        }

    }

    /// <summary>
    /// 拷贝相关文件，并返回字串（保存前时调用）
    /// </summary>
    public string CopyFile()
    {
        //string _folder = CommonLib.ComMon.BSconfig.GetConfig("AttachFileDir");
        string[] saveFilesInfo = Hdn_SaveFilesInfo.Value.Split(',');
        string[] savedDelFiles = Hdn_SavedDelFiles.Value.Split('|');
        string fileName = "";
        string fileRealName = "";
        string fileSize = "";
        string ret = "<files>";
        try
        {
            if (Hdn_SaveFilesInfo.Value != "" && saveFilesInfo.Length > 0)
            {
                string[] oneFileArr = null;
                for (int i = 0; i < saveFilesInfo.Length; i++)
                {
                    oneFileArr = saveFilesInfo[i].Split('|');
                    fileName = oneFileArr[0];
                    fileRealName = oneFileArr[1];
                    fileSize = oneFileArr[2];
                    if (File.Exists(Server.MapPath("../TmpFile/") + fileRealName))  //拷贝临时文件夹的文件
                    {
                        File.Copy(Server.MapPath("../TmpFile/") + fileRealName, ServerFolder + "/" + fileRealName, true);
                    }
                    ret += "<file><name>" + fileName + "</name><path>" + fileRealName + "</path><size>" + fileSize + "</size></file>";
                }
            }
            DeleteFile();
            if (Hdn_SavedDelFiles.Value != "" && savedDelFiles.Length > 0)
            {
                string oneDFileName = "";
                for (int j = 0; j < savedDelFiles.Length; j++)
                {
                    oneDFileName = savedDelFiles[j];
                    if (File.Exists(ServerFolder + "/" + oneDFileName))  //删除设定文件夹中的文件

                    {
                        File.Delete(ServerFolder + "/" + oneDFileName);
                    }
                }
            }
            if (ret == "<files>")
                ret = "";
            else
            {
                extBtnResh.AddScript("ReshAfterSave('saved');");//保存后重画table主要修改TYPE=1即可(文件连接路径修改)
                ret += "</files>";
            }
            return ret;
        }
        catch
        {
            return "-1";
        }

    }
}
